/*
 * config.cpp
 *
 *  Created on: 2011-7-25
 *      Author: minorityee1984@gmail.com
 *     Purpose: Agent Server配置
 *        Note:
 */

#include "config.h"
#include "global.h"
#include <assert.h>

#define NODE_SERVER		"server"
#define NODE_ADAPTER	"adapter"

#define ATTR_PORT		"port"
#define ATTR_MAXCLIENT	"maxclient"
#define ATTR_NAME		"name"
#define ATTR_BAUDRATE	"baudrate"
#define ATTR_BYTESIZE	"bytesize"
#define ATTR_PARITY		"parity"
#define ATTR_STOPBITS	"stopbits"
#define ATTR_TIMEOUT	"timeout"

CAgentServerConfig::CAgentServerConfig() : m_config(0)
{

}

int CAgentServerConfig::Initialize()
{
	TiXmlDocument doc;
	if(!doc.LoadFile(DEFAULT_AGENTSERVER_CONFIG, TIXML_ENCODING_UTF8))
	{
		WRITE_SERVER_LOG(enmLogLevel_ERROR, "Load config file:" DEFAULT_AGENTSERVER_CONFIG "failed!");
		return E_MISCONFIG;
	}
	TiXmlNode *pRoot = doc.FirstChild();
	if (NULL == pRoot)
	{
		WRITE_SERVER_LOG(enmLogLevel_ERROR, "Load root node failed!");
		return E_MSICONFIG;
	}
	int ret = LoadServerConfig(pRoot);
	if (0 > ret)
	{
		return ret;
	}
	ret = LoadAdapterConfig(pRoot);
	if (0 > ret)
	{
		return ret;
	}
	return S_OK;
}

int CAgentServerConfig::LoadServerConfig(TiXmlNode *pRoot)
{
	assert(NULl != pRoot);

	TiXmlElement *pServerConfig = pRoot->FirstChildElement(NODE_SERVER);
	if (NULL == pServerConfig)
	{
		WRITE_SERVER_LOG(enmLogLevel_ERROR, "Load pServerConfig failed!");
		return E_MISCONFIG;
	}

	const char* pszValue = NULL;
	pszValue = pServerConfig->Attribute(ATTR_PORT, &m_config.nPort);
	if (NULL == pszValue)
	{
		WRITE_SERVER_LOG(enmLogLevel_ERROR, "Load pServerConfig failed!");
		return E_MISCONFIG;
	}

	pszValue = pServerConfig->Attribute(ATTR_MAXCLIENT, (int*)&m_config.uMaxClient);
	if (NULL == pszValue)
	{
		WRITE_SERVER_LOG(enmLogLevel_ERROR, "Load pServerConfig failed!");
		return E_MISCONFIG;
	}
}

int CAgentServerConfig::LoadAdapterConfig(TiXmlNode *pRoot)
{
	assert(NULL != pRoot);

	TiXmlElement *pAdapterConfig = pRoot->FirstChildElement(NODE_ADAPTER);
	if (NULL == pAdapterConfig)
	{
		WRITE_SERVER_LOG(enmLogLevel_ERROR, "Load pAdapterConfig failed!");
		return E_MISCONFIG;
	}

	const char *pszValue = pAdapterConfig->Attribute(ATTR_PORT);
	if (NULL == pszValue)
	{
		WRITE_SERVER_LOG(enmLogLevel_ERROR, "Load port failed!");
		return E_MISCONFIG;
	}
	snprintf(m_serialport.szPortName, sizeof(m_serialport.szPortName), "%s", pszValue);

	pszValue = pAdapterConfig->Attribute(ATTR_BAUDRATE, &m_serialport.nBaudrate);
	if (NULL == pszValue)
	{
		WRITE_SERVER_LOG(enmLogLevel_ERROR, "Load baud rate failed!");
		return E_MISCONFIG;
	}

	pszValue = pAdapterConfig->Attribute(ATTR_BYTESIZE, &m_serialport.nByteSize);
	if (NULL == pszValue)
	{
		WRITE_SERVER_LOG(enmLogLevel_ERROR, "Load byte size failed!");
		return E_MISCONFIG;
	}

	const char *pszValue = pAdapterConfig->Attribute(ATTR_PARITY);
	if (NULL == pszValue)
	{
		WRITE_SERVER_LOG(enmLogLevel_ERROR, "Load parity failed!");
		return E_MISCONFIG;
	}
	m_serialport.chParity = *pszValue;

	pszValue = pAdapterConfig->Attribute(ATTR_BAUDRATE, &m_serialport.nStopBits);
	if (NULL == pszValue)
	{
		WRITE_SERVER_LOG(enmLogLevel_ERROR, "Load stop bits failed!");
		return E_MISCONFIG;
	}

	pszValue = pAdapterConfig->Attribute(ATTR_BYTESIZE, &m_serialport.nTimeout);
	if (NULL == pszValue)
	{
		WRITE_SERVER_LOG(enmLogLevel_ERROR, "Load timeout failed!");
		return E_MISCONFIG;
	}

	return S_OK;
}

